package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list=new ArrayList<>();
        list.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            list.add(begin);
        }
        String dataList= StringUtils.join(list, ",");
        List<Double> turnoverList=new ArrayList<>();

        for(LocalDate date:list){
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover=orderMapper.sumByMap(map);
            turnover=turnover==null?0:turnover;
            turnoverList.add(turnover);

        }
        return TurnoverReportVO.builder()
                .dateList(dataList)
                .turnoverList(StringUtils.join(turnoverList,','))
                .build();
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list=new ArrayList<>();
        list.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            list.add(begin);
        }
        String dataList= StringUtils.join(list, ",");
        List<Integer> newUserList=new ArrayList<>();
        List<Integer> totalUserList=new ArrayList<>();
        for(LocalDate date:list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map=new HashMap();
            map.put("end",endTime);
            Integer totalUser=userMapper.countByMap(map);
            map.put("begin",beginTime);
            Integer newUser=userMapper.countByMap(map);
            totalUserList.add(totalUser);
            newUserList.add(newUser);

        }
        return UserReportVO.builder()
                .dateList(dataList)
                .newUserList(StringUtils.join(newUserList,','))
                .totalUserList(StringUtils.join(totalUserList,','))
                .build();



    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list=new ArrayList<>();
        list.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            list.add(begin);
        }
        String dataList= StringUtils.join(list, ",");
        List<Integer> orderList=new ArrayList<>();
        List<Integer> validOrderList=new ArrayList<>();
        for(LocalDate date:list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map=new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            Integer order=orderMapper.countByMap(map);
            map.put("status", Orders.COMPLETED);
            Integer validOrder=orderMapper.countByMap(map);
            validOrderList.add(validOrder);
            orderList.add(order);

        }
        Integer totalOrderCount=orderList.stream().reduce(Integer::sum).get();
        Integer validOrderCount=validOrderList.stream().reduce(Integer::sum).get();
        double  orderCompleteRate=0.0 ;
        if(totalOrderCount!=0) {
           orderCompleteRate= validOrderCount.doubleValue() / totalOrderCount;
        }
        return  OrderReportVO.builder()
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .dateList(dataList)
                .orderCountList(StringUtils.join(orderList,','))
                .orderCompletionRate(orderCompleteRate)
                .validOrderCountList(StringUtils.join(validOrderList,','))
                .build();
    }

    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> salesTop10 =orderMapper.getSalesTop10(beginTime, endTime);
       List<String>names= salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
       List<Integer>numbers=salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
       return SalesTop10ReportVO.builder()
               .nameList(StringUtils.join(names,','))
               .numberList(StringUtils.join(numbers,','))
               .build();

    }
}
